package com.yingcheng.numerology.framework.bazi.core.util;

import com.yingcheng.numerology.framework.bazi.core.enums.EarthlyBranchesEnum;
import com.yingcheng.numerology.framework.bazi.core.enums.HeavenlyStemsEnum;
import java.util.LinkedList;
import java.util.List;

/**
 * 八字工具类
 *
 * @author xiaofeng
 * @date 2024/11/7 14:10
 */
public final class BaZiUtil {

    /**
     * 六十甲子表
     *
     * @return
     */
    public static List<String> listStemsAndBranches() {
        List<String> stemsAndBranchesTable = new LinkedList<>();
        for (int i = 0; i < 60; i++) {
            String heavenlyStemsName = HeavenlyStemsEnum.values()[i % 10].getName();
            String earthlyBranchesName = EarthlyBranchesEnum.values()[i % 12].getName();
            stemsAndBranchesTable.add(heavenlyStemsName + earthlyBranchesName);
        }
        return stemsAndBranchesTable;
    }

    public static String calculateYearHeavenlyStems(final Integer year, boolean hasGregorianCalendar) {
        // 是否公历
        if (hasGregorianCalendar) {
            // 天干计算方法
            String heavenlyStems = calculateGregorianCalendarHeavenlyStems(year);
            String earthlyBranches = calculateGregorianCalendarEarthlyBranches(year);
            return heavenlyStems + earthlyBranches;
        }
        // 计算阴历
        return null;
    }

    /**
     * 计算公历的天干
     *
     * @param year
     * @return
     */
    public static String calculateGregorianCalendarHeavenlyStems(final Integer year) {
        int index = (year - 3) % 10;
        if (index <= 0) {
            index += 10;
        }
        return HeavenlyStemsEnum.getByIndex(index).getName();
    }

    /**
     * 计算公历的地支
     *
     * @param year
     * @return
     */
    public static String calculateGregorianCalendarEarthlyBranches(final Integer year) {
        int index = (year - 3) % 12;
        if (index <= 0) {
            index += 12;
        }
        return EarthlyBranchesEnum.getByIndex(index).getName();
    }


    public static void main(String[] args) {
        List<String> list = BaZiUtil.listStemsAndBranches();
        System.out.println(list);
        String name = BaZiUtil.calculateYearHeavenlyStems(2023, true);
        System.out.println(name);
    }

}
